/**
 * @file caches.h
 *
 * The class for implementing caches
 *
 * @author Dahua Lin
 */

#ifdef _MSC_VER
#pragma once
#endif

#ifndef SLIPP_CACHES_H_
#define SLIPP_CACHES_H_

#include <slipp/base/basic_defs.h>
#include <slipp/base/lang_utils.h>

namespace sli
{

	template<class Gen, size_t N>
	class static_cache
	{
	public:
		typedef typename Gen::result_type value_type;
		static const size_t cache_size = N;

	public:
		static_cache(const Gen& gen)
		: m_gen(gen), m_current_idx(cache_size)
		{
		}

		value_type next()
		{
			if (m_current_idx == cache_size)
			{
				m_gen(m_data, cache_size);
				m_current_idx = 0;
			}

			return m_data[m_current_idx++];
		}

	private:
		SLIPP_ALIGN(32) value_type m_data[N];
		size_t m_current_idx;
		Gen m_gen;

	}; // end class static_cache



}

#endif
